package com.burpee.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.burpee.ui.Boarditems;

@Repository(value="boardItemsDao")
public class BoardItemsDaoImpl implements BoardItemsDao {

	SqlSessionTemplate sqlSessionTemplate;

	@Autowired
	public void setSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
	}

	public List<Boarditems> list() {
		List<Boarditems> boarditems = null;

		try {
			boarditems = sqlSessionTemplate.selectList("boarditemslist");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return boarditems;
	}

	public Boarditems view(long boardItemsNo) {

		Boarditems boarditems = null;

		try {
			boarditems = sqlSessionTemplate.selectOne("boarditemsview", boardItemsNo);
		} catch (Exception e) {

			e.printStackTrace();
		}

		return boarditems;
		//		// TODO Auto-generated method stub
		//		return null;
	}

	public int add(Boarditems boarditems) {
		int result = 0;

		try {
			result = (int)sqlSessionTemplate.insert("boarditemsadd", boarditems);
		} catch (Exception e) {

			e.printStackTrace();
		}

		return result;
	}

	public int modify(Boarditems boarditems) {
		int result = 0;

		try {

			result = (int)sqlSessionTemplate.update("boarditemsmodify", boarditems);

		} catch (Exception e) {
			e.printStackTrace();
		} 

		return result;	
		//		// TODO Auto-generated method stub
		//		return 0;
	}

	public int remove(long boardItemsNo) {
		int result = 0;

		try {

			result = (int)sqlSessionTemplate.update("boarditemsremove", boardItemsNo);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
		//		// TODO Auto-generated method stub
		//		return 0;
	}

	/////////////////아이템 카운트,검색//////////////////
	public int itemscounts(long boardItemNo) {
		int result = 0;

		try {

			result = (int)sqlSessionTemplate.update("boarditemscount", boardItemNo);

		} catch (Exception e) {
			e.printStackTrace();
		} 

		return result;
	}


	public List<Boarditems> itemviewnotice(long boardListNo) {

		List<Boarditems> result = null;

		try {
			result = sqlSessionTemplate.selectList("itemnotice",boardListNo);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public List<Boarditems> itemviewnotnotice(long boardListNo, long pageNo) {
		
		sqlSessionTemplate.selectList("itemnotnotice");
		Map<String, String> param = new HashMap<String, String>();

		param.put("boardListNo", Long.toString(boardListNo));
		param.put("pageNo", Long.toString(pageNo));

		List<Boarditems> result = null;

		try {
			result =sqlSessionTemplate.selectList("itemnotnotice", param);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public List<Boarditems> itemviewnotnoticeByTitle(long boardListNo,
			String title, long pageNo) {

		Map<String, String> param = new HashMap<String, String>();

		param.put("boardListNo", Long.toString(boardListNo));
		param.put("pageNo", Long.toString(pageNo));
		param.put("title", "%" + title + "%");

		List<Boarditems> result = null;

		try {
			result = sqlSessionTemplate.selectList("itemnotnoticebytitle", param);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public List<Boarditems> itemviewnotnoticeByContent(long boardListNo,
			String content, long pageNo) {
		Map<String, String> param = new HashMap<String, String>();

		param.put("boardListNo", Long.toString(boardListNo));
		param.put("pageNo", Long.toString(pageNo));
		param.put("content", "%" + content + "%");

		List<Boarditems> result = null;

		try {
			result = sqlSessionTemplate.selectList("itemnotnoticebycontent", param);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public List<Boarditems> itemviewnotnoticeByMemberName(long boardListNo,
			String memberName, long pageNo) {
		Map<String, String> param = new HashMap<String, String>();

		param.put("boardListNo", Long.toString(boardListNo));
		param.put("pageNo", Long.toString(pageNo));
		param.put("memberName", memberName + "%");

		List<Boarditems> result = null;

		try {
			result = sqlSessionTemplate.selectList("itemnotnoticebymembername", param);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
	
	public int countItemList(long boardListNo) {

		int count = 0;
		
		try {
			
			count = sqlSessionTemplate.selectOne("countitemlist", boardListNo);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public int countBoardItemNotNotice(long boardListNo) {

		int count = 0;
		
		try {
			count = sqlSessionTemplate.selectOne("countboarditemnotnotice", boardListNo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public int countBoardItemNotNoticeByTitle(long boardListNo, String title) {

		Map<String, String> param = new HashMap<String, String>();
		
		param.put("boardListNo", Long.toString(boardListNo));
		param.put("boardItemsTitle", "%" + title + "%");
		
		int count = 0;
		
		try {
			count = sqlSessionTemplate.selectOne("countboarditemnotnoticebytitle", param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public int countBoardItemNotNoticeByContent(long boardListNo,
			String content) {

		Map<String, String> param = new HashMap<String, String>();
		
		param.put("boardListNo", Long.toString(boardListNo));
		param.put("boardItemsView", "%" + content + "%");
		
		int count = 0;
		
		try {
			count = sqlSessionTemplate.selectOne("countboarditemnotnoticebycontent", param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public int countBoardItemNotNoticeByMemberName(long boardListNo,
			String memberName) {

		Map<String, String> param = new HashMap<String, String>();
		
		param.put("boardListNo", Long.toString(boardListNo));
		param.put("memberName", memberName);
		
		int count = 0;
		
		try {
			count = sqlSessionTemplate.selectOne("countboarditemnotnoticebymembername", param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return count;
	}
}
